package com.supcon.exchangedata.service.hj;

import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoArea;
import com.supcon.exchangedata.pojo.entity.oracle.BaseGeoAreaDetail;
import com.supcon.exchangedata.pojo.entity.oracle.GisPoint;
import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.GarbagePoint;
import com.supcon.exchangedata.pojo.entity.mysql.operation.hj.HjGarbageCleanPoint;
import com.supcon.exchangedata.repository.master.MasterBaseAreaRepository;
import com.supcon.exchangedata.repository.master.MasterBaseGeoAreaDetailRepository;
import com.supcon.exchangedata.repository.master.MasterGisPointRepository;
import com.supcon.exchangedata.repository.slave.SlaveGarBagePointVehicleRepository;
import com.supcon.exchangedata.repository.slave6.SlaveHjBasicInfo4;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * 垃圾清运点
 *
 * @Author: yangbin
 * @Date: 2022/03/17/15:54
 * @Description:
 */
@Service
@Slf4j
public class GarbagePointService {
    @Autowired
    private SlaveGarBagePointVehicleRepository slaveGarBagePointVehicleRepository;

    @Autowired
    private MasterBaseAreaRepository masterBaseAreaRepository;
    @Autowired
    private MasterGisPointRepository masterGisPointRepository;

    @Resource
    private MasterBaseGeoAreaDetailRepository masterBaseGeoAreaDetailRepository;
    @Resource
    private SlaveHjBasicInfo4 slaveHjBasicInfo4;

    /**
     * 垃圾清运点
     */
    @Transactional(rollbackFor = Exception.class)
    public void transGarbagePointData() {
        int count = 0;
        List<BaseGeoArea> baseGeoAreasByAreaType = masterBaseAreaRepository.findBaseGeoAreaByAreaType(4009L);
        List<BaseGeoArea> removeList = new ArrayList<>();
        List<BaseGeoArea> updateList = new ArrayList<>();
        List<GarbagePoint> all = slaveGarBagePointVehicleRepository.findAll();

        if (!CollectionUtils.isEmpty(baseGeoAreasByAreaType)) {
            baseGeoAreasByAreaType.forEach((e) -> {
                if (!CollectionUtils.isEmpty(all)) {
                    Iterator<GarbagePoint> iterator = all.iterator();
                    boolean removeFlag = true;
                    while (iterator.hasNext()) {
                        GarbagePoint next = iterator.next();
                        if (next.getCleanPointName().equals(e.getAreaName())) {
                            BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                            e.setBaseGeoAreaDetail(baseGeoAreaDetail);
                            removeFlag = false;
                            e.setAreaCode("hjljqyd" + next.getId());
                            e.setUpdateDate(LocalDateTime.now());
                            e.setAddress(next.getAddress());
                            e.setExtension3(next.getCleanPointCommunity());
                            e.setExtension1(next.getCleanPointAmount());
                            e.setExtension2(next.getCleanPointTime());
                            e.setExtension4(next.getCleanPointStreet());
                            if (!StringUtils.isEmpty(next.getCleanPointLatitude())) {
                                Double aLong = Double.valueOf(next.getCleanPointLatitude());
                                e.setLatitude(aLong);

                            }
                            if (!StringUtils.isEmpty(next.getCleanPointLongitude())) {
                                Double aLong = Double.valueOf(next.getCleanPointLongitude());
                                e.setLongitude(aLong);
                            }
                            e.getExtension4();
                            updateList.add(e);
                            iterator.remove();
                            break;
                        }
                    }
                    if (removeFlag) {
                        removeList.add(e);
                    }
                }
            });
        }
        deleteAreaAndPoint(removeList);
        updateAreaAndPointByCleanPoint(updateList);
        if (!CollectionUtils.isEmpty(all)) {
            int i = 0;
            for (GarbagePoint station : all) {
                BaseGeoArea area = new BaseGeoArea();
                GisPoint gisPoint = new GisPoint();
                i++;
                gisPoint.setCode("hjljqyd" + station.getId());
                area.setAreaCode("hjljqyd" + station.getId());
                area.setParentId(20948L);
                area.setAreaType(4009L);
                area.setAreaName(station.getCleanPointName());
                area.setFullLevelName("环境→清运点→" + station.getCleanPointName());
                area.setUpdator("管理员");
                area.setUpdateDate(LocalDateTime.now());
                area.setAddress(station.getAddress());
                area.setExtension3(station.getCleanPointCommunity());
                area.setExtension1(station.getCleanPointAmount());
                area.setExtension2(station.getCleanPointTime());
                gisPoint.setYwCategory("4009");
                if (!StringUtils.isEmpty(station.getCleanPointLatitude())) {
                    Double aLong = Double.valueOf(station.getCleanPointLatitude());
                    area.setLatitude(aLong);
                    gisPoint.setLatitude(aLong);
                }
                if (!StringUtils.isEmpty(station.getCleanPointLatitude())) {
                    Double aLong = Double.valueOf(station.getCleanPointLongitude());
                    area.setLongitude(aLong);
                    gisPoint.setLongitude(aLong);
                }
                gisPoint.setObjType(1L);
                gisPoint.setName(station.getCleanPointName());
                gisPoint.setObjCode("hjljqyd" + station.getId());
                gisPoint.setBusinessCategory("4009");
                gisPoint.setRoad(station.getCleanPointStreet());
                masterBaseAreaRepository.save(area);
                gisPoint.setObjId(area.getId());
                masterGisPointRepository.save(gisPoint);
                BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                baseGeoAreaDetail.setAreaId(area.getId());
                masterBaseGeoAreaDetailRepository.save(baseGeoAreaDetail);
            }

        }
    }


    /**
     * 清运点 集运点
     */
    public void tranGarbagePointDataNew() {
        long beginAll = System.currentTimeMillis();
        List<HjGarbageCleanPoint> all = slaveHjBasicInfo4.findAll();
        if (!CollectionUtils.isEmpty(all)) {
            List<BaseGeoArea> baseGeoAreaList = masterBaseAreaRepository.findBaseGeoAreaByAreaType(4009L);
            List<GisPoint> allByBusinessCategory = masterGisPointRepository.findAllByBusinessCategory("4009");
            List<BaseGeoAreaDetail> detailList = new ArrayList<>();
            if (!CollectionUtils.isEmpty(baseGeoAreaList)) {
                baseGeoAreaList.forEach((e) -> {
                    BaseGeoAreaDetail baseGeoAreaDetail = new BaseGeoAreaDetail();
                    baseGeoAreaDetail.setAreaId(e.getId());
                    //detailList.add(baseGeoAreaDetail);
                });
                long begin = System.currentTimeMillis();
                masterBaseAreaRepository.deleteBaseGeoAreaByAreaType(4009L);
                long end = System.currentTimeMillis();
                System.out.println("baseGeo删除时长：" + (end - begin) + "ms");

                long begin1 = System.currentTimeMillis();
                masterBaseGeoAreaDetailRepository.deleteInBatch(detailList);
                long end1 = System.currentTimeMillis();
                System.out.println("baseGeoDetail删除时长：" + (end1 - begin1) + "ms");
            }
            //删除对应表
            if (!CollectionUtils.isEmpty(allByBusinessCategory)) {
                long begin = System.currentTimeMillis();
                masterGisPointRepository.deleteGisPointByBusinessCategory("4009");
                long end = System.currentTimeMillis();
                System.out.println("gisPoint删除时长：" + (end - begin) + "ms");

            }
            List<BaseGeoArea> addBaseGeoList = new ArrayList<>();
            List<GisPoint> addGisPointList = new ArrayList<>();
            List<BaseGeoAreaDetail> addBaseGeoDetailList = new ArrayList<>();
            for (HjGarbageCleanPoint garbagePoint : all) {
                BaseGeoArea area = new BaseGeoArea();
                area.setAreaCode("hjljqyd" + garbagePoint.getId());
                area.setParentId(20948L);
                area.setAreaType(4009L);
                area.setAreaName(garbagePoint.getCleanPointName());
                area.setFullLevelName("环境→清运点→" + garbagePoint.getCleanPointName());
                area.setUpdator("jpa-program");
                String cleanPointLongitudeLatitude = garbagePoint.getCleanPointLongitudeLatitude();

                if (!StringUtils.isEmpty(cleanPointLongitudeLatitude)) {
                    String[] split = cleanPointLongitudeLatitude.split(",");
                    if (split.length == 2) {
                        String lonStr = split[0];
                        String latStr = split[1];
                        Double lon = Double.valueOf(lonStr);
                        area.setLongitude(lon);
                        Double lat = Double.valueOf(latStr);
                        area.setLatitude(lat);
                    }

                }
                area.setUpdateDate(LocalDateTime.now());
                area.setAddress(garbagePoint.getCleanPointCommunity() + garbagePoint.getCleanPointStreet());
                area.setExtension3(garbagePoint.getCleanPointCommunity());
                area.setExtension1(garbagePoint.getCleanPointAmount());
                area.setExtension4(garbagePoint.getCleanPointStreet());
                area.setExtension2(garbagePoint.getCleanPointTime());
                addBaseGeoList.add(area);
            }
            long begin = System.currentTimeMillis();
            List<BaseGeoArea> targetList = masterBaseAreaRepository.saveAll(addBaseGeoList);
            long end = System.currentTimeMillis();
            System.out.println("BaseGeoArea批量插入时长：" + (end - begin) + "ms");


            for (BaseGeoArea baseGeoArea : targetList) {
                if (baseGeoArea.getLongitude() != null && baseGeoArea.getLatitude() != null && baseGeoArea.getLongitude() > 119D && baseGeoArea.getLatitude() > 29D) {
                    GisPoint gisPoint = new GisPoint();
                    gisPoint.setObjId(baseGeoArea.getId());
                    gisPoint.setYwCategory("4009");
                    gisPoint.setObjType(1L);
                    gisPoint.setLongitude(baseGeoArea.getLongitude());
                    gisPoint.setLatitude(baseGeoArea.getLatitude());
                    gisPoint.setName(baseGeoArea.getAreaName());
                    gisPoint.setCode(baseGeoArea.getAreaCode());
                    gisPoint.setBusinessCategory("4009");
                    gisPoint.setRoad(baseGeoArea.getExtension4());
                    addGisPointList.add(gisPoint);
                }

            }


            long begin2 = System.currentTimeMillis();
            masterGisPointRepository.saveAll(addGisPointList);
            long end2 = System.currentTimeMillis();
            System.out.println("gisPoint批量插入时长：" + (end2 - begin2) + "ms");
            long endAll = System.currentTimeMillis();
            System.out.println("总耗时：" + (endAll - beginAll) / 1000 + "秒");
        }


    }


    /**
     * 更新垃圾清运点相关图层信息
     *
     * @param updateList
     */
    @Transactional(rollbackFor = Exception.class)
    public void updateAreaAndPointByCleanPoint(List<BaseGeoArea> updateList) {
        if (!CollectionUtils.isEmpty(updateList)) {
            updateList.forEach((e) -> {
                masterBaseAreaRepository.save(e);
                //更新gisPoint
                if (e.getLongitude() != null) {
                    GisPoint distinctByObjId = masterGisPointRepository.findDistinctByObjId(e.getId());
                    if (distinctByObjId == null) {
                        distinctByObjId = new GisPoint();
                    }
                    distinctByObjId.setObjId(e.getId());
                    distinctByObjId.setObjCode(e.getAreaCode());
                    distinctByObjId.setCode(e.getAreaCode());
                    distinctByObjId.setBusinessCategory(String.valueOf(e.getAreaType()));
                    distinctByObjId.setYwCategory(String.valueOf(e.getAreaType()));
                    distinctByObjId.setLatitude(e.getLatitude());
                    distinctByObjId.setLongitude(e.getLongitude());
                    distinctByObjId.setRoad(e.getAddress());
                    distinctByObjId.setName(e.getAreaName());
                    masterGisPointRepository.save(distinctByObjId);
                }
                BaseGeoAreaDetail distinctByAreaId = masterBaseGeoAreaDetailRepository.findDistinctByAreaId(e.getId());
                if (distinctByAreaId == null) {
                    distinctByAreaId = new BaseGeoAreaDetail();
                }
                BaseGeoAreaDetail baseGeoAreaDetail = e.getBaseGeoAreaDetail();
                if (baseGeoAreaDetail != null) {
                }
                distinctByAreaId.setAreaId(e.getId());
                masterBaseGeoAreaDetailRepository.save(distinctByAreaId);

            });
        }
    }

    /**
     * 删除不需要的图层点位信息
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteAreaAndPoint(List<BaseGeoArea> removeList) {
        if (!CollectionUtils.isEmpty(removeList)) {
            removeList.forEach((e) -> {
                masterBaseAreaRepository.deleteById(e.getId());
                masterGisPointRepository.deleteByObjIdAndYwCategory(e.getId(), String.valueOf(e.getAreaType()));
                masterBaseGeoAreaDetailRepository.deleteByAreaId(e.getId());
            });
        }
    }
}
